local assets = {
	Asset("ANIM", "anim/musha_appleplant.zip"),
	Asset("SOUND", "sound/common.fsb")
}

local prefabs = {
	"musha_apple"
}

local function onregenfn(inst)
	inst.AnimState:PlayAnimation("grow")
	inst.AnimState:PushAnimation("berry_idle", true)
end

local function makeemptyfn(inst)
	inst.AnimState:PushAnimation("picked")
	inst.AnimState:PlayAnimation("picking")
	inst.Light:Enable(false)
end

local function onpickedfn(inst)
	inst.AnimState:PlayAnimation("picking")
	inst.AnimState:PushAnimation("picked")
	inst.Light:Enable(false)
end

local function dig_up(inst, chopper)
	if inst.components.pickable:CanBePicked() then
		inst.components.lootdropper:SpawnLootPrefab("musha_apple")
	end
	inst.components.lootdropper:SpawnLootPrefab("musha_apple")

	inst:Remove()
end

local function fn()
	local inst = CreateEntity()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()
	inst.entity:AddLight()

	inst.Transform:SetTwoFaced()

	inst.AnimState:SetBank("worm")
	inst.AnimState:SetBuild("musha_appleplant")

	inst.AnimState:PlayAnimation("berry_idle", true)

	inst.Light:SetFalloff(.5)
	inst.Light:SetIntensity(.8)
	inst.Light:SetRadius(1.5)
	inst.Light:SetColour(1, 1, 1)
	inst.Light:Enable(true)
	--inst:AddTag("moonlight")

	inst.entity:SetPristine()
	if not TheWorld.ismastersim then
		return inst
	end

	inst.Transform:SetRotation(math.random() * 360)
	local s =
		RandomChoice(
		{
			0.75,
			0.8,
			0.85,
			0.9,
			0.95,
			1
		}
	)

	inst.Transform:SetScale(s, s, s)
	inst:AddComponent("pickable")
	inst.components.pickable.picksound = "dontstarve/wilson/pickup_reeds"
	inst.components.pickable.quickpick = true
	inst.components.pickable:SetUp("musha_apple", TUNING.TOTAL_DAY_TIME * 8)
	inst.components.pickable.onregenfn = onregenfn
	inst.components.pickable.onpickedfn = onpickedfn
	inst.components.pickable.makeemptyfn = makeemptyfn
	inst:AddComponent("lootdropper")
	inst:AddComponent("inspectable")

	inst:AddComponent("workable")
	inst.components.workable:SetWorkAction(ACTIONS.DIG)
	inst.components.workable:SetOnFinishCallback(dig_up)
	inst.components.workable:SetWorkLeft(1)

	MakeMediumBurnable(inst)
	MakeSmallPropagator(inst)
	MakeHauntableIgnite(inst)

	inst.SetBerry = function(isberry)
		if isberry then
			inst:RemoveComponent("workable")
			inst.components.pickable.onpickedfn = function()
				inst.Light:Enable(false)
				inst.AnimState:PlayAnimation("picked_wilt")
				inst:DoTaskInTime(1.25, inst.Remove)
			end
			inst:DoTaskInTime(
				8,
				function()
					if inst then
						inst.AnimState:PlayAnimation("picked_wilt")
						inst:DoTaskInTime(1.25, inst.Remove)
					end
				end
			)
		end
	end

	inst.SetPlant = function(isplant)
		if isplant then
			inst:WatchWorldState(
				"isspring",
				function(inst, isspring)
					if not isspring then
						inst.AnimState:PlayAnimation("picked_wilt")
						inst:DoTaskInTime(1.25, inst.Remove)
					end
				end
			)
		end
	end

	return inst
end

return Prefab("musha_appleplant", fn, assets, prefabs)
